/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#pragma once

#include <vector>
#include <map>
#include <string>
#include <exception>

#include "version.h"

/**
	Meta-Class for all exceptions Application may throw(native excluded).
*/
class AppException : public std::exception {
};

/**
	Exception for apps with invalid path.
*/
class AppNotFound : public AppException {
};

/**
	Exception for apps that contain no version.
*/
class NoVersions : public AppException {
};

/**
	Exception for apps with prohibited path.
*/
class InvalidDir : public AppException {
};

/**
	A single application.

	An application has:
	- name
	- path : location of all related files
	- versions
	- urls : one per version
	- description : multi line description
	- summary : one line summary
*/
class Application {

	private:

		std::string m_name, 										/// Name.
						m_path; 										/// Path
		std::map<std::string, Version> m_versions; 	/// Versions
		std::string m_description,								/// Description
						m_summary; 								/// Description summary

		/**
			Reload list of available versions.

			Clears and re-populares versions from database on disk.
			Versions can be found either by examining the subdirectories under the application folder, or the urls file.
		*/
		void refreshVersions();

		/**
			Remove all version information from the application.
		*/
		void clearVersions();

		/**
			Load description from file.
		*/
		void loadDescription();

	public:

		/**
			Constructor for creating useable objects.
			@param path Path to application folder.
		*/
		Application(const std::string path);

		Application() { }

		/**
			Default deconstructor.
		*/
		~Application() { }

		/**
			Application name.

			@return Application name.
		*/
		std::string name() const { return m_name; }

		/**
			Application path.

			@return Application full path.
		*/
		std::string path() const { return m_path; }

		/**
			List of all versions for this application.

			@return Vector of version objects.
		*/
		std::vector<Version> versions();

		/**
			Application description

			@return Application description.
		*/
		std::string description() const { return m_description; }

		/**
			Single line summary of application.

			@return Single line summary of application.
		*/
		std::string summary() const { return m_summary; }

		/**
			Last available version of application.

			@return Last available version.
		*/
		Version lastVersion();
};
